//
//  ByFootBallStandardPassView.m
//  caipiao_ios
//
//  Created by mc on 2018/8/4.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByFootBallStandardPassView.h"


@implementation ByFootBallStandardPassView

- (id)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        UILabel *titleLabel = [[UILabel alloc] init];
        [self addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.top.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-10);
            make.height.mas_equalTo(@20);
        }];
        titleLabel.font = FONT(14);
        titleLabel.textColor = BY_TEXT_COLOR;
        titleLabel.text = @"标准过关";
        
     
        
        UILabel *line = [[UILabel alloc] init];
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(0);
            make.right.equalTo(self).offset(0);
            make.bottom.equalTo(self).offset(0);
            make.height.mas_equalTo(@1);
        }];
        line.backgroundColor  = BY_LINE_COLOR;
        
    }
    return self;
}

- (void)setIsBasketBall:(BOOL)isBasketBall{
    _isBasketBall = isBasketBall;
}


- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    
    
    BOOL hasSingle = NO;
    
    if (_isBasketBall) {
        hasSingle = [ByBasketBallHandle hasSingleWithArray:dataArray];
    }else{
        hasSingle = [ByFootBallHandle hasSingleWithArray:dataArray];
    }
    
    
    CGFloat magin = 15;
    CGFloat width = (SCREEN_WIDTH - magin *5) / 4.0;
    CGFloat height = 30;
    
    NSArray *titlesAry;
    NSInteger count = 0;
    if (hasSingle){
        
        count = _dataArray.count;
        titlesAry = @[@"单关",@"2串1",@"3串1",@"4串1",@"5串1",@"6串1",@"7串1",@"8串1"];
        
    }else{
        
        count = _dataArray.count - 1;
        titlesAry = @[@"2串1",@"3串1",@"4串1",@"5串1",@"6串1",@"7串1",@"8串1"];
    }
    
    self.btnsArray = [NSMutableArray arrayWithCapacity:1];
    for (int i = 0; i < count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:btn];
        btn.frame = CGRectMake(magin + i % 4 * (width + magin), 50 + i / 4 * (height + 10), width, height);
        [btn setTitle:titlesAry[i] forState:UIControlStateNormal];
        [btn setBackgroundColor:rgba(245, 245, 245, 1)];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = FONT(14);
        
        if (hasSingle){
            
            if ([_selectedArray containsObject:[NSNumber numberWithInt:i + 1]]) {
                btn.backgroundColor = BY_THEME_COLOR;
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                btn.selected = YES;
            }
            
        }else{
            
            if ([_selectedArray containsObject:[NSNumber numberWithInt:i + 2]]) {
                btn.backgroundColor = BY_THEME_COLOR;
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                btn.selected = YES;
            }
        }
        
       
        
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnsArray addObject:btn];
    }
}

- (void)setSelectedArray:(NSMutableArray *)selectedArray{
    _selectedArray = selectedArray;
}


- (void)btnAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        
        btn.backgroundColor = BY_THEME_COLOR;
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }else{
        
        [btn setBackgroundColor:rgba(245, 245, 245, 1)];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    
    NSMutableArray *selectedArray = [NSMutableArray arrayWithCapacity:1];
    BOOL hasSingle = NO;
    
    if (_isBasketBall) {
        hasSingle = [ByBasketBallHandle hasSingleWithArray:self.dataArray];
    }else{
        hasSingle = [ByFootBallHandle hasSingleWithArray:self.dataArray];
    }
    
    for (int i = 0; i < self.btnsArray.count; i++) {
        UIButton *btn = [self.btnsArray objectAtIndex:i];
        if (btn.selected) {
            if (hasSingle) {
                 [selectedArray addObject:[NSNumber numberWithInt:i + 1]];
            }else{
                 [selectedArray addObject:[NSNumber numberWithInt:i + 2]];
            }
           
        }
    }
             
    NSInteger count = 0;
    for (NSNumber * number in selectedArray) {
        if (!_isBasketBall) {
            NSInteger value = [ByFootBallHandle getCountWithSelectedCount:_dataArray.count withNumber:number.integerValue withArray:self.dataArray];
            count = count + value;
        }else{
            NSInteger value = [ByFootBallHandle getBasketBallCountWithSelectedCount:_dataArray.count withNumber:number.integerValue withArray:self.dataArray];
            count = count + value;
        }
        
        
    }
    self.selectBlock(count,selectedArray);
    
    
}






@end
